package com.virtium.vpc.action.controller;

import java.util.ArrayList;
import java.util.Iterator;

import com.virtium.vpc.dao.DAOFactory;
import com.virtium.vpc.dao.VpcDensityDAO;
import com.virtium.vpc.hibernate.model.VpcDensity;

public class VpcDensityManager {
	private static ArrayList<VpcDensity> arrVpcDensity = null;

	public static ArrayList<VpcDensity> getAll() {
		try {
			if (arrVpcDensity != null && arrVpcDensity.size() > 0)
				return arrVpcDensity;
			VpcDensityDAO VpcDensityDAO = DAOFactory.getInstance()
					.getVpcDensityDAO();
			arrVpcDensity = new ArrayList<VpcDensity>();
			arrVpcDensity.addAll(VpcDensityDAO.findAll());
		} catch (Exception ex) {
		}
		return arrVpcDensity;
	}
	
	public static String getTextByID(int id){
		getAll();
		Iterator<VpcDensity> iterObject = arrVpcDensity.listIterator();
		while(iterObject.hasNext()){
			VpcDensity object = iterObject.next();
			if(object.getId().equals(id))return object.getText();
			
		}
		return "";
	}
	
	public static void AddNew(String text){
		try{
			VpcDensity object = new VpcDensity();
			object.setText(text);
			VpcDensityDAO ObjectDAO = DAOFactory.getInstance().getVpcDensityDAO();
			ObjectDAO.save(object);
			arrVpcDensity = null;
		}catch(Exception ex){
			
		}
	}
	
	public static void Delete(int id){
		try{
			
			VpcDensityDAO ObjectDAO = DAOFactory.getInstance().getVpcDensityDAO();
			VpcDensity object = ObjectDAO.findById(id);
			ObjectDAO.delete(object);
			arrVpcDensity = null;
		}catch(Exception ex){
			
		}
	}
}
